
package FILE_SENDING;

import GUI.PrivateChatPanel;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 * This Class Hamdles all the send file operations
 * It connects to it's creator
 * @author Kalif Yaniv
 */
public class SendFile implements Runnable
{
    JFileChooser _choosewindow;
    File _file;
    String _DestIP;
    ServerSocket socket;
    PrivateChatPanel _creator;
    boolean _cont = false;
    /**
     * Constructor for SendFile
     * @param creator - The creator Panel (PrivateChatPanel)
     */
    public SendFile(PrivateChatPanel creator)
    {
                    _creator = creator;
                    _choosewindow = new JFileChooser();
                    int returnval = _choosewindow.showOpenDialog(null);
                    if (returnval==JFileChooser.APPROVE_OPTION)
                    {
                                try
                                {
                                    _file = _choosewindow.getSelectedFile();
                                    if(_file.length()>=6022386)
                                    {
                                        _creator.RecieveControlMessage("Sorry ! You can only send files up to 6 MB\n your file size is:  "+_file.length()/((float)(1024 * 1024))+" MB");
                                    }
                                    else
                                    {
                                        _creator.SendMessage("I want to send you a File.\n\t The file is: "+_file.getName()+"\n\t  click \"Recieve File\" to recieve it");
                                        _cont = true;
                                    }
                                }
                                catch (IOException ex)
                                {
                                    Logger.getLogger(PrivateChatPanel.class.getName()).log(Level.SEVERE, null, ex);
                                }

                    }
    }
    /**
     * This Method Runs This Thread
     * It sends the file to the specified Destination
     */
    public void run()
    {
        try {
            if(_cont==true)
            {
                    //_DestIP = IP;
                    socket = new ServerSocket(9999); /*_DestIP,*/
                    //Open socket for the file transformation
                    Thread.sleep(1000); //Wait a little bit
                    Socket s = socket.accept();
                    /////SEND THE FILE/////
                    _creator.RecieveControlMessage("Sendind File :" + _file.getName() + "..."); //UPDATE The sender "Start Sending"
                    byte[] mybytearray = new byte[(int) _file.length()];
                    FileInputStream fis = new FileInputStream(_file);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    bis.read(mybytearray, 0, mybytearray.length);
                    OutputStream os = s.getOutputStream();
                    os.write(mybytearray, 0, mybytearray.length);
                    os.flush();
                    _creator.RecieveControlMessage("The File " + _file.getName() + " Has Been  Successfully Sent . . .");
                    socket.close();
                    s.close();
                    _creator.SetISSending(false); //The File has been succesfully sent
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(SendFile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SendFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

}
